﻿@using CodeGenerator
@using CodeGenerator.Models.TemplateModels
@inherits RazorEngine.Templating.TemplateBase<CodeGenerator.Models.TemplateModels.DtoFileModel>
@@{ ViewBag.Title = "@Model.CnName"; Layout = "~/Views/Shared/_LayoutPlugin.cshtml"; }
@@section css{
<style>
    .tree-btn {
        padding: 0 10px;
    }

    .img-preview {
        /* datagrid上的图片 */
        max-height: 24px;
        max-width: 64px;
    }

    .div-tree-toolbar {
        padding: 5px 10px;
        background-color: rgb(252, 252, 252);
    }

    .easyui-layout .panel-header {
        background-color: rgb(239, 239, 239);
        color: black;
        padding: 5px 15px;
        border: 1px solid rgb(197, 197, 197);
    }

        .easyui-layout .panel-header .panel-title {
            font-weight: normal;
        }

    ul.list-unstyled > li {
        margin-top: 5px;
    }

    .form-box {
        width: 200px;
    }

    .form-lb {
        display: inline-block;
        text-align: right;
        width: 100px;
    }

        .form-lb::after {
            content: ':';
        }

    #imgPreview {
        /* 编辑窗体上的图片 */
        width: auto;
        height: auto;
        max-width: 200px;
        max-height: 150px;
        /* outline: 1px solid gray; */
    }

    .dg-table {
        min-width: 460px;
    }

    .dg-title tr td {
        padding: 2px 0 3px 2px;
    }

        .dg-title tr td:nth-child(odd) {
            text-align: right;
        }

        .dg-title tr td:nth-child(even) {
            text-align: left;
        }
</style>
}

<div id="dlg@(Model.Name)">
    <div title="基本信息" data-options="style:{borderWidth:0}, border:false,closable:false, pill:true,border:false">
        <form id="form@(Model.Name)" method="post" enctype="multipart/form-data">

            <input type="hidden" name="id">
            <input type="hidden" name="timeStamp">
            <div data-options="region:'center',border:false">
                <ul class="list-unstyled">
                    @{
                        var j = Model.ClassPropertys.Count > 16 ? 3 : 2;//确定每行几列
                        for (int i = 0; i < Model.ClassPropertys.Count; i += j)
                        {
                            <li>
                                @{
                                    for (int n = 0; n < j; n++)
                                    {
                                        if (i + n < Model.ClassPropertys.Count)
                                        {
                                            ClassProperty item = Model.ClassPropertys[i + n];
                                            <span class="form-lb">@item.CnName</span>
                                            <input id="@item.Name.FirstCharToLower()" name="@item.Name.FirstCharToLower()" class="easyui-textbox form-box" data-options="">
                                        }
                                    }
                                }
                            </li>
                        }
                    }
                </ul>
            </div>
        </form>
    </div>
</div>
<div class="dlg-@(Model.Name)-btns">
    <button class="btn btn-success" onclick="main.save()">
        <i class="fa fa-check"></i><span id="sSave">保存并新增</span>
    </button>
    <button onclick="main.close()" class="btn btn-primary">
        <i class="fa fa-close"></i>关闭
    </button>
</div>

@@section js{
<script src="/Content/EasyUI/extension/jquery.edatagrid.js"></script>
<script src="/Content/global/scripts/util.js"></script>
<script src="~/Content/EasyUI/extension/datagrid-dnd.js"></script>
<script>

        var main = {
            init: function () {
                var _this = this;
                _this.loadDialog();
            },//初始化
            search: function () {
                var _this = main;
                _this.loadData();
            },
            loadDialog: function () {
                $('#dlg@(Model.Name)').dialog({
                    fit:true,
                    noheader:true,
                    doSize: true,
                    modal: true,
                    buttons: '.dlg-@(Model.Name)-btns',
                    onClose: function () {
                        $('#form@(Model.Name)').form("clear");
                        lytClose();
                    }
                });
            },
            open@(Model.Name)AddDialog: function () {
                document.getElementById("sSave").innerHTML = "保存并新增";
            },
            open@(Model.Name)EditDialog: function () {
                document.getElementById("sSave").innerHTML = "保存";
                let selectedRow = baseWindow.$('#dg').datagrid('getSelected');
                $('#form@(Model.Name)').form('load', selectedRow);
            },
            save: function () {
                var _this = this;
                // 表单
                let isValid = $('#form@(Model.Name)').form('validate');
                if (!isValid) return;

                let formInfo = $('#form@(Model.Name)').form('getJsonByFormData');

                $.ajax({
                    url: '/@(Model.Name)/CreateOrUpdate',
                    type: "POST",
                    data:  formInfo ,
                    success: function (d) {
                        if (d.success) {

                            $('#dg').datagrid('reload');
                            //保存并清空，只保留分类
                            $('#form@(Model.Name)').form('clear');

                            //如果是编辑，则保存后关闭编辑窗口
                            if (edit) {
                                _this.close();
                                edit = false;
                            }
                        } else {
                           baseWindow.layer.msg(d.message, {icon: 5});
                        }
                    }
                });
            },
            close: function () {
                $('#dlg@(Model.Name)').dialog('close');
                edit = false;
            }
        };
        function baseInit({ act ,baseWindow}) {
            main.init();
            if (baseWindow)
                window.baseWindow = baseWindow;
            if (act == 1) {
                main.open@(Model.Name)AddDialog();
            } else if (act == 2) {
                main.open@(Model.Name)EditDialog();
            }
        }

</script>
}